<?php
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_COMPONENT.DS.'persistance'.DS.'DAOWord.php');

class DAOWordImpl implements DAOWord{
	
	public function updateWord($con, $dtoWord){
		$sql = "UPDATE #__hizkuntzak_words SET word_es='$dtoWord->word_es', ";
		$sql = $sql."word_eu='$dtoWord->word_eu', ";
		$sql = $sql."active=$dtoWord->active WHERE id=$dtoWord->idWord ";

		$con->setQuery($sql);
		
		if(!$con->query()){
			$con->stderr();
			return false;
		}
		else
			return true;
	}
	
	public function getWord($con, $id){
		$sql = "select * from #__hizkuntzak_words where (id=$id)";
		
		$con->setQuery($sql);
		
    	$row = $con->loadObject();
    	
    	if($row == null){
    		return null;
    	}
    	$word = new DTOWord();
    	
    	$word->setIdWord($row->id);
        $word->setWordEs($row->word_es);
        $word->setWordEu($row->word_eu); 
        $word->setActive($row->active);
        
        return $word;
	}
	
	public function getWords($con, $active){
		$results = array();
		
		$sql = "select * from #__hizkuntzak_words ";
		
		if($active){
			$sql = $sql. "where (active=1)";
		}
		
		$con->setQuery($sql);
		
    	$objectList = $con->loadObjectList();

        foreach($objectList as $row) {

        	$word = new DTOWord();
        	
        	$word->setIdWord($row->id);
        	$word->setWordEs($row->word_es);
        	$word->setWordEu($row->word_eu); 
        	$word->setActive($row->active);
            
        	$results[] = $word;
          } 
		
		return $results;
	}
	
	public function addWord($conn, $dtoWord){
		
		$data = new stdClass();
		$data->id = null;
		$data->word_es = $dtoWord->getWordEs();
		$data->word_eu = $dtoWord->getWordEu();
		$data->active = $dtoWord->isActive();
		
		if(!$conn->insertObject( '#__hizkuntzak_words', $data, 'id' )){
			echo $conn->stderr();
			return false;
		}
		else 
			return true;
	}

}